class Valleyers::Show::Homes::Show::PostsController < ApplicationController
  def index
    @valleyer = Valleyer.find params[:id]
    @post = Post.new :valleyer_id => params[:id],
      :created_by_valleyer_id => current_valleyer_id
  end

  def new
    @post = Post.new :valleyer_id => params[:valleyer_id],
      :post_session_id => params[:session_id],
      :created_by_valleyer_id => current_valleyer_id
    @valleyer = Valleyer.find params[:valleyer_id]
  end

  def create
    @post = Post.new params[:post]
    if @post.post_session.nil?
      @post.post_session = PostSession.new :valleyer_id => params[:post][:valleyer_id],
        :created_by_valleyer_id => current_valleyer_id
      @post.post_session.save
    end
    @post.save
    @valleyer = @post.valleyer
    @old_post = @post
    @post = Post.new :valleyer_id => @post.valleyer_id,
      :post_session_id => @post.post_session_id,
      :created_by_valleyer_id => current_valleyer_id
  end

  def destroy
    @post = Post.find params[:id]
    @valleyer = @post.valleyer
    if @post.post_session.posts.length == 1
      @post.post_session.destroy
    end
    @post.destroy
    @post = Post.new :valleyer_id => @post.valleyer_id,
      :post_session_id => @post.post_session_id,
      :created_by_valleyer_id => current_valleyer_id
  end
end
